package com.chenjt.jvm.third.vmops;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;

/**
 * 注释：
 *
 * -Xms100m -Xmx100m
 *
 * 监控我们的应用服务器堆内存使用情况，设置一些阈值进行报警等处理
 *
 * @author chenjt
 * @date 2025/7/14 9:48
 */
public class MemoryMonitor {

    public static void main(String[] args) {
        MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage usage = memoryBean.getHeapMemoryUsage();
        System.out.println("INIT HEAP：" + usage.getInit() / 1024 / 1024 + "m");
        System.out.println("MAX HEAP：" + usage.getMax() / 1024 / 1024 + "m");
        System.out.println("USE HEAP：" + usage.getUsed() / 1024 / 1024 + "m");
        System.out.println("\nFull Information:");
        System.out.println("Heap Memory Usage：" + memoryBean.getHeapMemoryUsage());
        System.out.println("Non-Heap Memory Usage：" + memoryBean.getNonHeapMemoryUsage());

        System.out.println("=============通过java来获取相关系统状态=============");
        System.out.println("当前堆内存大小totalMemory "  + (int) Runtime.getRuntime().totalMemory() / 1024 / 1024 + "m");
        System.out.println("空闲堆内存大小freeMemory "  + (int) Runtime.getRuntime().freeMemory() / 1024 / 1024 + "m");
        System.out.println("最大可用总堆内存maxMemory "  + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "m");

    }


}
